<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Connects to a hostname with optionally asyncronous DNS resolving</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="eventbufferevent.connect.html">EventBufferEvent::connect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="eventbufferevent.construct.html">EventBufferEvent::__construct</a></div>
 <div class="up"><a href="class.eventbufferevent.html">EventBufferEvent</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="eventbufferevent.connecthost" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">EventBufferEvent::connectHost</h1>
  <p class="verinfo">(PECL event &gt;= 1.2.6-beta)</p><p class="refpurpose"><span class="refname">EventBufferEvent::connectHost</span> &mdash; <span class="dc-title">Connects to a hostname with optionally asyncronous DNS resolving</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-eventbufferevent.connecthost-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">bool</span> 
   <span class="methodname"><strong>EventBufferEvent::connectHost</strong></span>
    ( <span class="methodparam">
    <span class="type"><a href="class.eventdnsbase.html" class="type EventDnsBase">EventDnsBase</a></span>
     <code class="parameter">$dns_base</code>
   </span>
   , <span class="methodparam">
    <span class="type">string</span>
     <code class="parameter">$hostname</code>
   </span>
   , <span class="methodparam">
    <span class="type">int</span>
     <code class="parameter">$port</code>
   </span>
   [, <span class="methodparam">
    <span class="type">int</span>
     <code class="parameter">$family</code>
    <span class="initializer"> = EventUtil::AF_UNSPEC</span>
   </span>
  ] )</div>

  <p class="para rdfs-comment">
   Resolves the DNS name hostname, looking for addresses of type
   <em><code class="parameter">family</code></em>
   (
   <em>EventUtil::AF_*</em>
   constants). If the name resolution fails, it invokes the event callback
   with an error event. If it succeeds, it launches a connection attempt just
   as
   <span class="methodname"><a href="eventbufferevent.connect.html" class="methodname">EventBufferEvent::connect()</a></span>
   would.
  </p>
  <p class="para">
   <em><code class="parameter">dns_base</code></em>
   is optional. May be <strong><code>NULL</code></strong>, or an object created with
   <span class="methodname"><a href="eventdnsbase.construct.html" class="methodname">EventDnsBase::__construct()</a></span>
   . For asyncronous hostname resolving pass a valid event dns base resource.
   Otherwise the hostname resolving will block.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <a href="class.eventdnsbase.html" class="classname">EventDnsBase</a>
    is available only if
    <em>Event</em>
    configured
    <strong class="option configure">--with-event-extra</strong>

    (
    <em>event_extra</em>
    library,
    <em class="emphasis">libevent protocol-specific functionality support including HTTP, DNS, and RPC</em>
    ).
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="methodname"><strong>EventBufferEvent::connectHost()</strong></span>
    requires
    <em>libevent-2.0.3-alpha</em>
    or greater.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-eventbufferevent.connecthost-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>

     <em><code class="parameter">dns_base</code></em>
    </dt>

    <dd>

     <p class="para">
      Object of
      <a href="class.eventdnsbase.html" class="classname">EventDnsBase</a>
      in case if DNS is to be resolved asyncronously. Otherwise <strong><code>NULL</code></strong>.
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">hostname</code></em>
    </dt>

    <dd>

     <p class="para">
      Hostname to connect to. Recognized formats are:
<div class="example-contents screen"><div class="parameterscode"><pre class="parameterscode">www.example.com (hostname)
 1.2.3.4 (ipv4address)
 ::1 (ipv6address)
[::1] ([ipv6address])</pre>
</div></div>
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">port</code></em>
    </dt>

    <dd>

     <p class="para">
      Port number
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">family</code></em>
    </dt>

    <dd>

     <p class="para">
      Address family.
      <strong><code>EventUtil::AF_UNSPEC</code></strong>
      ,
      <strong><code>EventUtil::AF_INET</code></strong>
      , or
      <strong><code>EventUtil::AF_INET6</code></strong>
      . See
      <a href="class.eventutil.html#eventutil.constants" class="link">EventUtil constants</a>
      .
     </p>
    </dd>

   
  </dl>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-eventbufferevent.connecthost-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <strong><code>TRUE</code></strong> on success. Otherwise <strong><code>FALSE</code></strong>.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-eventbufferevent.connecthost-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4162">
   <p><strong>Example #1 
    <span class="function"><strong>EventBufferEvent::connectHost()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Read&nbsp;callback&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">readcb</span><span style="color: #007700">(</span><span style="color: #0000BB">$bev</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$base</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//$input&nbsp;=&nbsp;$bev-&gt;input;&nbsp;//$bev-&gt;getInput();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;//$pos&nbsp;=&nbsp;$input-&gt;search("TTP");<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">input</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">search</span><span style="color: #007700">(</span><span style="color: #DD0000">"TTP"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">input</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">remove</span><span style="color: #007700">(</span><span style="color: #0000BB">$buf</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1024</span><span style="color: #007700">))&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Event&nbsp;callback&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">eventcb</span><span style="color: #007700">(</span><span style="color: #0000BB">$bev</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$events</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$base</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$events&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">::</span><span style="color: #0000BB">CONNECTED</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connected.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$events&nbsp;</span><span style="color: #007700">&amp;&nbsp;(</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">::</span><span style="color: #0000BB">ERROR&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">::</span><span style="color: #0000BB">EOF</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$events&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">::</span><span style="color: #0000BB">ERROR</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"DNS&nbsp;error:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getDnsErrorString</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Closing\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$base</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exit</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Done\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$base&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventBase</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$dns_base&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventDnsBase</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;We'll&nbsp;use&nbsp;async&nbsp;DNS&nbsp;resolving<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$dns_base</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Failed&nbsp;to&nbsp;init&nbsp;DNS&nbsp;Base\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$bev&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">/*&nbsp;use&nbsp;internal&nbsp;socket&nbsp;*/&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">::</span><span style="color: #0000BB">OPT_CLOSE_ON_FREE&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">EventBufferEvent</span><span style="color: #007700">::</span><span style="color: #0000BB">OPT_DEFER_CALLBACKS</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"readcb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">/*&nbsp;writecb&nbsp;*/&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"eventcb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$base<br /></span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$bev</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Failed&nbsp;creating&nbsp;bufferevent&nbsp;socket\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//$bev-&gt;setCallbacks("readcb",&nbsp;/*&nbsp;writecb&nbsp;*/&nbsp;NULL,&nbsp;"eventcb",&nbsp;$base);<br /></span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">enable</span><span style="color: #007700">(</span><span style="color: #0000BB">Event</span><span style="color: #007700">::</span><span style="color: #0000BB">READ&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">Event</span><span style="color: #007700">::</span><span style="color: #0000BB">WRITE</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">output</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//$bev-&gt;getOutput();<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$output</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"GET&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]}</span><span style="color: #DD0000">&nbsp;HTTP/1.0\r\n"</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">\r\n"</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"<br /></span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Failed&nbsp;adding&nbsp;request&nbsp;to&nbsp;output&nbsp;buffer\n"</span><span style="color: #007700">);<br />}<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connectHost</span><span style="color: #007700">(</span><span style="color: #0000BB">$dns_base</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">EventUtil</span><span style="color: #007700">::</span><span style="color: #0000BB">AF_UNSPEC</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Can't&nbsp;connect&nbsp;to&nbsp;host&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$base</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dispatch</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Connected.
HTTP/1.0 301 Moved Permanently
Location: http://www.google.co.uk/
Content-Type: text/html; charset=UTF-8
Date: Sat, 09 Mar 2013 12:21:19 GMT
Expires: Mon, 08 Apr 2013 12:21:19 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

&lt;HTML&gt;&lt;HEAD&gt;&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=utf-8&quot;&gt;
&lt;TITLE&gt;301 Moved&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;
&lt;H1&gt;301 Moved&lt;/H1&gt;
The document has moved
&lt;A HREF=&quot;http://www.google.co.uk/&quot;&gt;here&lt;/A&gt;.
&lt;/BODY&gt;&lt;/HTML&gt;
Closing
Done
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-eventbufferevent.connecthost-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member">
    <span class="methodname"><a href="eventbufferevent.connect.html" class="methodname" rel="rdfs-seeAlso">EventBufferEvent::connect()</a> - Connect buffer event's file descriptor to given address or
  UNIX socket</span>
   </li>
  </ul>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="eventbufferevent.connect.html">EventBufferEvent::connect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="eventbufferevent.construct.html">EventBufferEvent::__construct</a></div>
 <div class="up"><a href="class.eventbufferevent.html">EventBufferEvent</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
